package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import Bean.Info;
import Bean.User;
import sql.JDBCUtil;

public class InfoDao {
	public void insert(Info info) throws Exception {
		Connection con = null;
		PreparedStatement ps = null;
		String sql = "insert into info value(?, ?, ?);";
		try {
			con = JDBCUtil.getcon();
			ps = con.prepareStatement(sql);
			ps.setString(1, info.getIn_info());
			ps.setString(2, info.getOut_info());
			ps.setString(3, info.getCar_id());
			ps.executeUpdate();
		} catch(SQLException e) {
			throw new RuntimeException(e);
		} finally {
			JDBCUtil.close(null, ps, con);
		}
	}
	public Info selectByCarId(String car_id) throws Exception {
		Info info = null;
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select * from info where car_id = ?;";
		try {
			con = JDBCUtil.getcon();
			ps = con.prepareStatement(sql);
			ps.setString(1, car_id);
			rs = ps.executeQuery();
			if(rs.next()) {
				info = new Info();
				info.setIn_info(rs.getString("in_info"));
				info.setOut_info(rs.getString("out_info"));
				info.setCar_id(rs.getString("car_id"));
			}
		} catch(SQLException e) {
			throw new RuntimeException(e);
		} finally {
			JDBCUtil.close(rs, ps, con);
		}
		return info;
	}
	public void dropByCarid(String car_id) throws Exception {
		Connection con = null;
		PreparedStatement ps = null;
		String sql = "delete from info where car_id = ?";
		try {
			con = JDBCUtil.getcon();
			ps = con.prepareStatement(sql);
			ps.setString(1, car_id);
			ps.executeUpdate();
		} catch(SQLException e) {
			throw new RuntimeException(e);
		} finally {
			JDBCUtil.close(null, ps, con);
		}
	}
	public void update(Info info) throws Exception {
		Connection con = null;
		PreparedStatement ps = null;
		String sql = "update info set in_info = ?, out_info = ? where car_id = ?";
		try {
			con = JDBCUtil.getcon();
			ps = con.prepareStatement(sql);
			ps.setString(1, info.getIn_info());
			ps.setString(2, info.getOut_info());
			ps.setString(3, info.getCar_id());
			ps.executeUpdate();
		} catch(SQLException e) {
			throw new RuntimeException(e);
		} finally {
			JDBCUtil.close(null, ps, con);
		}
	}
}
